﻿using Dapper;
using System;
using BCEntity.MakeStaticPagingComponent.PageTemplateAttachment;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.MakeStaticPagingComponent.PageTemplateAttachment
{
    /// <summary>
    /// 页面模板附件
    /// </summary>
    public class PageTemplateAttachmentData : IPageTemplateAttachmentData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM pagetemplateattachment";
        private const string QUERY = @"SELECT PageTemplateAttachmentId,UpdateTime,EditAdminId,EditAdminName,PageTemplateId,WebsiteTemplateId,FileType,InsertLocation,FileAddress,CreateAdminId,CreateAdminName,CreateTime FROM pagetemplateattachment";
        public PageTemplateAttachmentData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查页面模板附件是否存在
        public async Task<bool> Exists(int pageTemplateAttachmentId)
        {
            string sql = string.Format(@"{0} WHERE PageTemplateAttachmentId=?PageTemplateAttachmentId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageTemplateAttachmentId", pageTemplateAttachmentId, DbType.Int32);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加页面模板附件
        public async Task<PageTemplateAttachmentEntity> Add(PageTemplateAttachmentEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("pagetemplateattachment", new string[] { "PageTemplateId", "WebsiteTemplateId", "FileType", "InsertLocation", "FileAddress",
                "CreateAdminId", "CreateAdminName" });
            sql = string.Format("{0} {1} WHERE PageTemplateAttachmentId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageTemplateId", entity.PageTemplateId, DbType.Int64, size: 20);
            parameters.Add("?WebsiteTemplateId", entity.WebsiteTemplateId, DbType.Int32, size: 11);
            parameters.Add("?FileType", entity.FileType.ToString(), DbType.AnsiString);
            parameters.Add("?InsertLocation", entity.InsertLocation.ToString(), DbType.AnsiString);
            parameters.Add("?FileAddress", entity.FileAddress?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<PageTemplateAttachmentEntity>(sql, parameters, databaseContext.Transaction);

        }
        #endregion

        #region 修改页面模板附件
        public async Task<PageTemplateAttachmentEntity> Update(PageTemplateAttachmentEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("pagetemplateattachment", new string[] { "EditAdminId", "EditAdminName", "PageTemplateId", "WebsiteTemplateId", "FileType",
                "InsertLocation", "FileAddress" }, new string[] { "PageTemplateAttachmentId" });
            sql = string.Format("{0} {1} WHERE PageTemplateAttachmentId=?PageTemplateAttachmentId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageTemplateAttachmentId", entity.PageTemplateAttachmentId, DbType.Int32, size: 11);
            parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
            parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?PageTemplateId", entity.PageTemplateId, DbType.Int64, size: 20);
            parameters.Add("?WebsiteTemplateId", entity.WebsiteTemplateId, DbType.Int32, size: 11);
            parameters.Add("?FileType", entity.FileType.ToString(), DbType.AnsiString);
            parameters.Add("?InsertLocation", entity.InsertLocation.ToString(), DbType.AnsiString);
            parameters.Add("?FileAddress", entity.FileAddress?.Trim(), DbType.AnsiString, size: 1000);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<PageTemplateAttachmentEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除页面模板附件
        public async Task<bool> Delete(int pageTemplateAttachmentId)
        {
            string sql = @"DELETE FROM pagetemplateattachment WHERE PageTemplateAttachmentId=?PageTemplateAttachmentId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageTemplateAttachmentId", pageTemplateAttachmentId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取页面模板附件
        public async Task<PageTemplateAttachmentEntity> GetEntity(int pageTemplateAttachmentId)
        {
            string sql = string.Format(@"{0} WHERE PageTemplateAttachmentId=?PageTemplateAttachmentId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageTemplateAttachmentId", pageTemplateAttachmentId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<PageTemplateAttachmentEntity>(sql, parameters);
        }
        #endregion

        #region 获取页面模板附件列表
        public async Task<IEnumerable<PageTemplateAttachmentEntity>> GetList(long pageTemplateId)
        {
            string sql = string.Format("{0} WHERE PageTemplateId=?PageTemplateId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageTemplateId", pageTemplateId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryAsync<PageTemplateAttachmentEntity>(sql, parameters);
        }
        #endregion

        #region 获取页面模板附件分页
        public async Task<(IEnumerable<PageTemplateAttachmentEntity>, int)> Query(long pageTemplateId,DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            wheres.Add("PageTemplateId=?PageTemplateId");
            parameters.Add("?PageTemplateId", pageTemplateId, DbType.Int64, size: 20);
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM pagetemplateattachment {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryMultipleAsync<PageTemplateAttachmentEntity, int>(sql, parameters);
        }
        #endregion
    }
}



